package com.qf.controller;

import com.qf.pojo.Student;
import com.qf.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @Description: 公用常量
 * @Data: 2022/9/15 15:13
 * @Pacakge: com.qf.controller
 * @ClassName: StudentController
 * @Author: fan
 */
@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    StudentService studentService;

    /**
     * 查询所有学生
     * @return
     */
    @RequestMapping("/findAll")
    public ModelAndView findAll(){
        List<Student> list =  studentService.findAll();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("student");
        modelAndView.addObject("students",list);
        return modelAndView;
    }

    /**
     *新增学生
     * @param student 学生实体
     * @return
     */
    @RequestMapping("/insert")
    public String insert(Student student){
        studentService.updateOrInsert(student);
        return"redirect:/student/findAll";
    }

    /**
     * 修改用户
     * @param student
     * @return
     */
    @RequestMapping("/update")
    public String update(Student student){
        studentService.updateOrInsert(student);
        return"redirect:/student/findAll";
    }

    /**
     * 删除页面
     * @param student
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public String delete(@RequestBody Student student){
        studentService.deleteById(student.getId());
        return "success";
    }

    /**
     * 修改之前进行查询
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/findById")
    public String findById(Integer id, Model model){
       Student student = studentService.findById(id);
       model.addAttribute("student",student);
       return "update";
    }
    @RequestMapping("/add")
    public String add(){
        return "insert";
    }
}

